<template>
	<view>
		<button @click="fn1">点我，扫码</button>
		
		<button @click="onStart">开始录音</button>
		<button @click="onStop">结束录音</button>
	</view>
</template>

<script lang="ts" setup>
	import { onLoad } from '@dcloudio/uni-app'
	
	let recorderManager: UniApp.RecorderManager = null
	let audioCtx: UniApp.InnerAudioContext = null
	
	onLoad(() => {
		// 1. 获取到录音管理器对象
		recorderManager  =  uni.getRecorderManager()
		// 2. 再获取到音频管理器上下文对象
		audioCtx = uni.createInnerAudioContext()
		// 2. 做 onStop 的监听
		recorderManager.onStop((res) => {
			console.log('录音停止', res)
			
			// 设置 audioCtx.src 为当前录音的临时文件
			audioCtx.src = res.tempFilePath
			// 开始播放
			audioCtx.play()
		})
	})
	
	const fn1 = () => {
		uni.scanCode({
			success: (res) => {
				console.log('success', res)
			},
			fail: (res) => {
				console.log('fail', res)
			}
		})
	}
	
		
	const onStart = () => {
		recorderManager.start({})
		
		// uni.startRecord({
		// 	success: (res) => {
		// 		console.log('start success', res)
				
		// 		// 直接播放
		// 		uni.playVoice({
		// 			filePath: res.tempFilePath
		// 		})
		// 	}
		// })
	}
	
	const onStop = () => {
		recorderManager.stop()
		// uni.stopRecord()
	}
</script>

<style>

</style>